package top.linyuxb.lycloud.config.starter.config;

import cn.hutool.extra.spring.EnableSpringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import top.linyuxb.lycloud.common.common.LyConfigJdbcService;
import top.linyuxb.lycloud.common.properties.LyConfigProperties;
import top.linyuxb.lycloud.config.starter.common.LyConfigBeanPostProcessor;
import top.linyuxb.lycloud.config.starter.controller.LyConfController;
import top.linyuxb.lycloud.config.starter.event.MyStartupListener;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/5/31 16:51
 * @Desc:
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(LyConfigProperties.class)
@EnableSpringUtil
@Import({MyStartupListener.class, LyConfController.class})
@ConditionalOnProperty(
        prefix = "ly-config",
        name = "open",
        havingValue = "true"
)
public class LyConfig {
    @Resource
    private LyConfigProperties lyRpcClientProperties;

    @Bean(value = "lyConfigBeanPostProcessor")
    public BeanPostProcessor beanPostProcessor() {
        LyConfigJdbcService.initDataSource(lyRpcClientProperties);
        return new LyConfigBeanPostProcessor(lyRpcClientProperties);
    }

}
